package pd.struct;

import java.util.ArrayList;
import java.util.Collection;

/**
 * An ordered collection of script trees.
 *
 * @author Joshua
 */
public class OrderedScriptForest extends ScriptForest
{
    /**
     * Initializes a new ordered script forest.
     */
    public OrderedScriptForest()
    {
        super( new ArrayList<ScriptTree>() );
    }

    /**
     * Initializes a new ordered script forest and inserts the values of the collection into the forest.
     *
     * @param trees the trees in the forest, in their natural order.
     */
    public OrderedScriptForest( Collection<ScriptTree> trees )
    {
        super( new ArrayList<ScriptTree>() );

        add( trees );
    }

    /**
     * Returns the tree at the specified index.
     *
     * @param index the index of the tree to be returned.
     * @return the tree at the specified index.
     */
    public final ScriptTree get( int index )
    {
        ScriptTree[] trees = super.getTrees();

        if( index >= 0 && index < trees.length )
            return trees[index];
        else
            return null;
    }

    /**
     * Removes and returns the tree at the specified index.
     *
     * @param index the index of the tree to remove from the forest.
     * @return the tree at the specified index if it was successfully removed, null otherwise.
     */
    public final ScriptTree remove( int index )
    {
        if( index >= 0 && index < size() )
        {
            ScriptTree toRemove = get( index );

            if( remove( toRemove ))
                return toRemove;
            else
                return null;
        }
        else
            return null;
    }
}
